package com.example.config;

//Mybatis  的配置类
//作用：配置Mybatis，替代之前的mybatis-config.xml+mybatisUtils

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisConfig {
//    配置sqlSessionFactory
    @Bean
    public SqlSessionFactory sqlSessionFactory(DruidDataSource druidDataSource)throws Exception{
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(druidDataSource);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScaner = new MapperScannerConfigurer();
        mapperScaner.setBasePackage("com.example.dao");
        return mapperScaner;
    }
}
